
	

	$( "#lista_gestores" ).change(function() {
		editarMenusGestor();
	});
	
	$("#select_menu").change(function(){
		listarSubMenus();
	});
	
	$('#listadoMenusSubMenus').nestable(); //hace editable la lista de submenus
	
	init_gestores(); //cargamos los gestores en el input select
	
	

var editor = function () {
	/** Formulario crear nuevo gestor **/
	var nou = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true">'+
		   	'<div class="modal-dialog" >'+
				'<div class="modal-content">'+
					'<div class="modal-header"><button class="close" data-dismiss="modal">×</button>'+
						'<h3>Nou Gestor</h3>'+
					'</div>'+
					'<div class="modal-body">'+
						'<div class="row">'+
							'<div class="col-md-12">'+
					   			'<form class="form-horizontal row-border" id="form_editar_usuario">'+
				          		  '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Nom gestor</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="nombre"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Descripció</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="descripcion"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Vista portada</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="vista_portada"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Color</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="color"  class="form-control"><a href="'+url_base+'info_plantilla">Colores</a>'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Icono</label>'+
				              		  '<div class="col-sm-6">'+
				              		 	'<input type="text" id="icono"  class="form-control"><a href="'+url_base+'info_plantilla">Iconos</a>'+
				               	  	 '</div>'+
					              '</div>'+
						'</div>'+
					'</div>'+
					'<div class="modal-footer">'+
			  				'<button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button>'+
			  				'<button type="button" onClick="javascript:crearNouGestor();" data-dismiss="modal" class="btn btn-primary">Crear</button>'+
			  		'</div>'+
				'</div>'+
			'</div>'+
		'</div>');
	/** Formulario editar gestor ya existente **/
	var edit = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true">'+
		   	'<div class="modal-dialog" >'+
				'<div class="modal-content">'+
					'<div class="modal-header"><button class="close" data-dismiss="modal">×</button>'+
						'<h3>Editar gestor</h3>'+
					'</div>'+
					'<div class="modal-body">'+
						'<div class="row">'+
							'<div class="col-md-12">'+
					   			'<form class="form-horizontal row-border" id="form_editar_usuario">'+
				          		  '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Nom gestor</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="mod_nombre"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Descripció</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="mod_descripcion"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Vista portada</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="mod_vista_portada"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Color</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="mod_color"  class="form-control"><a href="'+url_base+'info_plantilla">Colores</a>'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Icono</label>'+
				              		  '<div class="col-sm-6">'+
				              		 	'<input type="text" id="mod_icono"  class="form-control"><a href="'+url_base+'info_plantilla">Iconos</a>'+
				               	  	 '</div>'+
					              '</div>'+
						'</div>'+
					'</div>'+
					'<div class="modal-footer">'+
			  				'<button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button>'+
			  				'<button type="button" onClick="javascript:editor.confirmar();" data-dismiss="modal" class="btn btn-danger">Eliminar</button>'+
			  				'<button type="button" onClick="javascript:modificarGestor();" data-dismiss="modal" class="btn btn-success">Modificar</button>'+
			  		'</div>'+
				'</div>'+
			'</div>'+
		'</div>');
	/* Formulario para crear nuevo menu */
	var formNouMenu = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true">'+
		   	'<div class="modal-dialog" >'+
				'<div class="modal-content">'+
					'<div class="modal-header"><button class="close" data-dismiss="modal">×</button>'+
						'<h3>Nou Menú</h3>'+
					'</div>'+
					'<div class="modal-body">'+
						'<div class="row">'+
							'<div class="col-md-12">'+
					   			'<form class="form-horizontal row-border" id="form_editar_usuario">'+
				          		  '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Nom gestor</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="nombre_menu"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Rol usuari</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<select id="permiso_menu"><option value="3">Usuari</option><option value="2">Admin</option><option value="1">Root</option></select>'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Ruta</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="ruta_menu"  placeholder="Exemple : jocnet/crear_noticia" class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Icono</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="icono_menu"  placeholder="Exemple : fa-cog" class="form-control"><a href="'+url_base+'info_plantilla">Iconos</a>'+
				              		  '</div>'+
					              '</div>'+
						'</div>'+
					'</div>'+
					'<div class="modal-footer">'+
			  				'<button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button>'+
			  				'<button type="button" onClick="javascript:crearMenuNou();" data-dismiss="modal" class="btn btn-info">Crear</button>'+
			  		'</div>'+
				'</div>'+  
			'</div>'+
		'</div>');
	/** Formulario para editar menu ya existente **/
	var formEditMenu = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true">'+
		   	'<div class="modal-dialog" >'+
				'<div class="modal-content">'+
					'<div class="modal-header"><button class="close" data-dismiss="modal">×</button>'+
						'<h3>Editar menú</h3>'+
					'</div>'+
					'<div class="modal-body">'+
						'<div class="row">'+
							'<div class="col-md-12">'+
					   			'<form class="form-horizontal row-border" id="form_editar_usuario">'+
				          		  '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Nom</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="edit_nombre_menu"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Rol usuari</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<select id="edit_permiso_menu"><option value="3">Usuari</option><option value="2">Admin</option><option value="1">Root</option></select>'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Ruta</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="edit_ruta_menu"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Icono</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="edit_icono_menu"  class="form-control"><a href="'+url_base+'info_plantilla">Iconos</a>'+
				               	  	 '</div>'+
					              '</div>'+
					            '</form>'+
						'</div>'+
					'</div>'+
					'<div class="modal-footer">'+
			  				'<button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button>'+
			  				'<button type="button" onClick="javascript:editor.eliminarMenu();" data-dismiss="modal" class="btn btn-danger">Eliminar</button>'+
			  				'<button type="button" onClick="javascript:editarMenu();" data-dismiss="modal" class="btn btn-success">Modificar</button>'+
			  		'</div>'+
				'</div>'+
			'</div>'+
		'</div>');
	/** Pantalla confirmacio eliminar gestor **/
	var confirmarEliminacion = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog" ><div class="modal-content"><div class="modal-header"><button class="close" data-dismiss="modal">×</button><h3>Confirmar confirmació gestor</h3></div><div class="modal-body"><div class="row"><div class="col-md-12">Confirmar eliminació</div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button><button type="button" onClick="javascript:eliminarGestor();" data-dismiss="modal" class="btn btn-danger">Confirmar</button></div></div></div></div>');
	/** Pantalla confirmacio eliminar menu **/
	var confirmarEliminacionMenu = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog" ><div class="modal-content"><div class="modal-header"><button class="close" data-dismiss="modal">×</button><h3>Confirmar confirmació menú</h3></div><div class="modal-body"><div class="row"><div class="col-md-12">Confirmar eliminación</div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button><button type="button" onClick="javascript:deleteMenu();" data-dismiss="modal" class="btn btn-danger">Confirmar</button></div></div></div></div>'); 
	/** Pantalla confirmacion eliminar submenu **/
	var confirmarEliminacionSubMenu = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog" ><div class="modal-content"><div class="modal-header"><button class="close" data-dismiss="modal">×</button><h3>Confirmar confirmació Sub menú</h3></div><div class="modal-body"><div class="row"><div class="col-md-12">Confirmar eliminacion</div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button><button type="button" onClick="javascript:eliminarSubMenu();" data-dismiss="modal" class="btn btn-danger">Confirmar</button></div></div></div></div>'); 
	/* Formulario para crear nuevo menu */
	var formNouSubMenu = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true">'+
		   	'<div class="modal-dialog" >'+
				'<div class="modal-content">'+
					'<div class="modal-header"><button class="close" data-dismiss="modal">×</button>'+
						'<h3>Nou Sub-Menú</h3>'+
					'</div>'+
					'<div class="modal-body">'+
						'<div class="row">'+
							'<div class="col-md-12">'+
					   			'<form class="form-horizontal row-border" id="form_editar_usuario">'+
				          		  '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Nom Submenu</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="nombre_submenu"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Rol usuari</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<select id="permiso_submenu"><option value="3">Usuari</option><option value="2">Admin</option><option value="1">Root</option></select>'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Ruta</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="ruta_submenu"  placeholder="Ejemplo : jocnet/crear_noticia" class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
						'</div>'+
					'</div>'+
					'<div class="modal-footer">'+
			  				'<button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button>'+
			  				'<button type="button" onClick="javascript:crearNouSubmenu();" data-dismiss="modal" class="btn btn-info">Crear</button>'+
			  		'</div>'+
				'</div>'+  
			'</div>'+
		'</div>');
	/** Formulario para editar SubMenu ya existente **/
	var formEditSubMenu = $('<div class="modal fade " style="top:15%;" tabindex="-1" role="dialog" aria-hidden="true">'+
		   	'<div class="modal-dialog" >'+
				'<div class="modal-content">'+
					'<div class="modal-header"><button class="close" data-dismiss="modal">×</button>'+
						'<h3>Editar Sub menú</h3>'+
					'</div>'+
					'<div class="modal-body">'+
						'<div class="row">'+
							'<div class="col-md-12">'+
					   			'<form class="form-horizontal row-border" id="form_editar_usuario">'+
				          		  '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Nom</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="edit_nombre_submenu"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Rol usuari</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<select id="edit_permiso_submenu"><option value="3">Usuari</option><option value="2">Admin</option><option value="1">Root</option></select>'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Ruta</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="edit_ruta_submenu"  class="form-control">'+
				               	  	 '</div>'+
					              '</div>'+
					              '<div class="form-group">'+
				              	    '<label class="col-sm-3 control-label">Icono</label>'+
				              		  '<div class="col-sm-6">'+
				                   		 '<input type="text" id="edit_icono_submenu"  class="form-control"><a href="'+url_base+'info_plantilla">Iconos</a>'+
				               	  	 '</div>'+
					              '</div>'+
					            '</form>'+
						'</div>'+
					'</div>'+
					'<div class="modal-footer">'+
			  				'<button type="button" class="btn btn-default" data-dismiss="modal">Tancar</button>'+
			  				'<button type="button" onClick="javascript:editor.eliminarSubMenu();" data-dismiss="modal" class="btn btn-danger">Eliminar</button>'+
			  				'<button type="button" onClick="javascript:editarSubMenu();" data-dismiss="modal" class="btn btn-success">Modificar</button>'+
			  		'</div>'+
				'</div>'+
			'</div>'+
		'</div>');
	return {
		nou: function(){
			nou.modal(); //inicia formulario nou gestor
		},
		form: function(){
			edit.modal(); //formulario edicion gestor
		},
		hideNou:function(){
			nou.modal('hide');
		},
		hideForm: function(){
			edit.modal('hide');
		},
		confirmar:function(){
			confirmarEliminacion.modal();
		},
		hideConfirmar:function(){
			confirmarEliminacion.modal('hide');
		},
		formNouMenu:function(){
			formNouMenu.modal(); //formulario nuevo menu
		},
		hideFormNouMenu:function(){
			formNouMenu.modal('hide');
		},
		formEditMenu:function(){
			formEditMenu.modal(); //formulario editar menu
		},
		hideFormEditMenu:function(){
			formEditMenu.modal('hide');
		},
		formNouSubMenu:function(){
			formNouSubMenu.modal();
		},
		eliminarMenu:function(){
			confirmarEliminacionMenu.modal();
		},
		hideEliminarMenu:function(){
			confirmarEliminacionMenu.modal('hide');
		},
		formEditSubMenu:function(){
			formEditSubMenu.modal();
		},
		hideFormEditSubMenu:function(){
			formEditSubMenu.modal('hide');
		},
		eliminarSubMenu:function(){
			confirmarEliminacionSubMenu.modal();
		},
		hideEliminarSubMenu:function(){
			confirmarEliminacionSubMenu.modal('hide');
		}
	};
}();


function init_gestores(){
	$.ajax({
	    url: url_base+"get_all_gestores",
	    type: "POST",			      
		beforeSend: function() {
		    fcf.cargando();	
		}, 
		success: function(result){
			var gestores = result.gestores;
			fcf.hidecargando();
			$.each(gestores, function( key, value ) {
				$('#lista_gestores').append('<option value="'+value.id+'" >'+value.nombre+'</option>');
			});
			editarMenusGestor(); //carga las opciones del primer gestor que cargamos al iniciar la pagina
	 	}
	});
}


function editarMenusGestor(){

	$("#menus_listado").html('');
	$("#select_menu").html('');
	
	var id_gestor = $("#lista_gestores").val();

	$.ajax({
	    url: url_base+"get_menus_por_idgestor",
	    data:{id:id_gestor},
	    type: "POST",			      
		    beforeSend: function() {
	
		}, 
		success: function(result){

			$.each(result.menus, function( key, value ) {
				$('#select_menu').append('<option value="'+value.id+'" >'+value.nombre+'</option>');
			});
			listarSubMenus();
			$("#menus_listado").append(result.listado_menus);

			$('.edicion_menus').editable({
				validate: function(value) {
					if($.trim(value) == '') return 'El camp no pot estar buit';
				}
			});
	 	}
	});
	$("#listado_menus_gestor").show();
}
	
function listarSubMenus(){
		var id_menu = $('#select_menu').val();
		$("#lista_submenus").empty();
		$.ajax({
		    url: url_base+"get_submenus_por_idmenu",
		    data:{id:id_menu},
		    type: "POST",			      
			    beforeSend: function() {
			    fcf.cargando();
			}, 
			success: function(result){
				fcf.hidecargando();
				$.each(result, function( key, value ) {
					$('#lista_submenus').append('<option value="'+value.id+'" >'+value.nombre+'</option>');
				});
		 	}
		});
}


function crearNouGestor(){

	var nombre_gestor = $("#nombre").val();
	var descripcion = $("#descripcion").val();
	var vista_portada = $("#vista_portada").val();
	var color = $("#color").val();
	var icono = $("#icono").val();
	
	
	$.ajax({
	    url: url_base+"crear_gestor",
	    data:{nombre:nombre_gestor,descripcion:descripcion,vista:vista_portada,color:color,icono:icono},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();	
		}, 
		success: function(result){
			fcf.hidecargando();
			if(result == true){
				editor.hideNou();
				fcf.showText("Gestor creat correctament",true);
			}else{
				fcf.showText("Error crear gestor",false);
			}
	 	}
	});
	
}

function editarGestor(){
	
	editor.form(); //iniciamos formulario

	var id_gestor = $("#lista_gestores").val();
	
	var aux = id_gestor;
	
	$.ajax({
	    url: url_base+"get_gestor_por_id",
	    data:{id:id_gestor},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();	
		}, 
		success: function(result){
			fcf.hidecargando();
			 $("#mod_nombre").val(result.nombre);
			 $("#mod_descripcion").val(result.descripcion);
			 $("#mod_vista_portada").val(result.vista_portada);
			 $("#mod_color").val(result.color);
			 $("#mod_icono").val(result.icono);
	 	}
	});
}

function eliminarGestor(){

	var id_gestor = $("#lista_gestores").val();
	
	$.ajax({
	    url: url_base+"eliminar_gestor",
	    data:{id:id_gestor},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();	
		}, 
		success: function(result){
			fcf.hidecargando();
			if(result == true){
				fcf.showText("Gestor eliminat correctament",true);
			}else{
				fcf.showText("Error eliminar gestor",false);
			}
	 	}
	});
}

function modificarGestor(){

	var id_gestor = $("#lista_gestores").val();
	var nombre_gestor = $("#mod_nombre").val();
	var descripcion = $("#mod_descripcion").val();
	var vista_portada = $("#mod_vista_portada").val();
	var color = $("#mod_color").val();
	var icono = $("#mod_icono").val();
	

	$.ajax({
	    url: url_base+"modificar_gestor",
	    data:{id:id_gestor,nombre:nombre_gestor,descripcion:descripcion,vista:vista_portada,color:color,icono:icono},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();	
		}, 
		success: function(result){
			fcf.hidecargando();
			if(result == true){
				fcf.showText("Gestor modificar correctament",false);
				
			}else{
				fcf.showText("Error modificació gestor",false);
			}
			
	 	}
	});
}


function crearMenuNou(){
	
	var id_gestor = $("#lista_gestores").val();
	var nombre = $("#nombre_menu").val();
	var permiso_menu = $("#permiso_menu").val();
	var ruta_menu = $("#ruta_menu").val();
	var icono_menu = $("#icono_menu").val();


	$.ajax({
	    url: url_base+"crear_nou_menu",
	    data:{id_gestor:id_gestor,nombre:nombre,permiso:permiso_menu,ruta:ruta_menu,icono:icono_menu},
	    type: "POST",			      
		    beforeSend: function() {
		}, 
		success: function(result){
			if(result == true){
				editarMenusGestor();
				editor.hideFormNouMenu();
			}else{
				fcf.showText("Error al crear menú",false);
			}
	 	}
	});
}


function formEditarMenu(){
	
	var id_menu = $("#select_menu").val();
	
	editor.formEditMenu();
		
	$.ajax({
	    url: url_base+"info_menu_id",
	    data:{id:id_menu},
	    type: "POST",			      
		    beforeSend: function() {
		}, 
		success: function(result){
			var info = $.parseJSON(result);
			
			$.each(info, function( key, obj ) {
				$("#edit_nombre_menu").val(obj.nombre);
				$("#edit_ruta_menu").val(obj.enlace);
				$("#edit_icono_menu").val(obj.icono);
			});
	 	}
	});
}

function editarMenu(){
	
	var id_menu = $("#select_menu").val();
	
	var edit_nombre = $("#edit_nombre_menu").val();
	var edit_permiso = $("#edit_permiso_menu").val();
	var edit_ruta = $("#edit_ruta_menu").val();
	var edit_icono = $("#edit_icono_menu").val();
	
	$.ajax({
	    url: url_base+"editar_menu",
	    data:{id_menu:id_menu,nombre:edit_nombre,permiso:edit_permiso,icono:edit_icono,ruta:edit_ruta},
	    type: "POST",			      
		    beforeSend: function() {
		}, 
		success: function(result){
			
			if(result == true){
				editarMenusGestor();
			}else{
				fcf.showText("Error editar menú",false);
			}
	 	}
	});
}

function deleteMenu(){

	var id_menu = $("#select_menu").val();
	
	$.ajax({
	    url: url_base+"eliminar_menu",
	    data:{id:id_menu},
	    type: "POST",			      
		    beforeSend: function() {
		}, 
		success: function(result){
			
			if(result >= 1){
				fcf.showText("Menú eliminat correctament",true);
				editarMenusGestor();
				editor.hideEliminarMenu();
				editor.hideFormEditMenu();
			}else{
				fcf.showText("Error eliminar menú",false);
			}
	 	}
	}); 
}



function crearNouSubmenu(){
	
	var id_gestor = $("#lista_gestores").val();
	var id_menu = $("#select_menu").val();
	var nombre = $("#nombre_submenu").val();
	var permiso = $("#permiso_submenu").val();
	var ruta = $("#ruta_submenu").val();

	
	$.ajax({
	    url: url_base+"crear_submenu",
	    data:{id_gestor:id_gestor,parent:id_menu,nombre:nombre,permiso:permiso,ruta:ruta},
	    type: "POST",			      
		    beforeSend: function() {
		}, 
		success: function(result){
			if(result == true){
				editarMenusGestor();
			}else{

			}
	 	}
	});
}

function formEditarSubMenu(){
	
	var id = $("#lista_submenus").val();
	
	editor.formEditSubMenu();
	
	$.ajax({
	    url: url_base+"info_submenu_id",
	    data:{id:id},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();
		}, 
		success: function(result){
			fcf.hidecargando();
			var info = $.parseJSON(result);
			$.each(info, function( key, obj ) {
				$("#edit_nombre_submenu").val(obj.nombre);
				$("#edit_ruta_submenu").val(obj.enlace);
				$("#edit_icono_submenu").val(obj.icono);
			});
	 	}
	});
	
	

}

function editarSubMenu(){
	
	var id_menu = $("#lista_submenus").val();
	var edit_nombre = $("#edit_nombre_submenu").val();
	var edit_permiso = $("#edit_permiso_submenu").val();
	var edit_ruta = $("#edit_ruta_submenu").val();
	var edit_icono = $("#edit_icono_submenu").val();
	
	$.ajax({
	    url: url_base+"editar_menu",
	    data:{id_menu:id_menu,nombre:edit_nombre,permiso:edit_permiso,icono:edit_icono,ruta:edit_ruta},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();
		}, 
		success: function(result){
			
			if(result == true){
				editarMenusGestor();
			}else{
				fcf.hidecargando();
				fcf.showText("Error editar menú",false);
			}
	 	}
	});
}

function eliminarSubMenu(){
	
	var id_submenu = $("#lista_submenus").val();
	
	$.ajax({
	    url: url_base+"eliminar_menu",
	    data:{id:id_submenu},
	    type: "POST",			      
		    beforeSend: function() {
		    fcf.cargando();
		}, 
		success: function(result){
			fcf.hidecargando();
			if(result >= 1){
				fcf.showText("Menú eliminat correctament",false);
				editarMenusGestor();
				editor.hideEliminarMenu();
				editor.hideFormEditMenu();
			}else{
				fcf.showText("Error eliminar menú",false);
			}
	 	}
	}); 
}


function guardarCambiosListadoSubmenus(){
	
	var lista_submenus = $('#listadoMenusSubMenus').nestable('serialize');
	var id_menu = $("#select_menu").val();
	var ordenMenu = 1;
	var info = '[';
	
	
	$.each( lista_submenus, function( key, obj ) {
		var id_submenu = obj.id;
		var nombre_menu = $("#submenu"+obj.id).html();
		var subMenus = '[';
		var ordenSubMenu = 1;
		if(typeof obj.children !== 'undefined' && obj.children.length > 0){
			
			$.each(obj.children, function( key, obj ) {
				subMenus += '{"id_submenu":"'+obj.id+'","nombre_submenu":"'+$("#submenu"+obj.id).html()+'","orden":"'+ordenSubMenu+'"},';
				ordenSubMenu++;
			});
			subMenus = subMenus.substr(0,subMenus.length-1);
			subMenus += "]";
			info += '{"id_menu":"'+id_submenu+'","nombre_menu":"'+nombre_menu+'","orden":"'+ordenMenu+'","submenus":'+subMenus+'},';
		}else{
			info += '{"id_menu":"'+id_submenu+'","nombre_menu":"'+nombre_menu+'","orden":"'+ordenMenu+'"},';
		}
		
		ordenMenu++;	

	});

	info = info.substr(0,info.length-1);
	info += "]";
	

	
	$.ajax({
	    url: url_base+"editar_submenus",
	    data:{info:info,id_menu:id_menu},
	    type: "POST",			      
		    beforeSend: function() {
		}, 
		success: function(result){
			
			if(result == true){
			    fcf.showText("Ordre canviat correctament.",false);
			    editarMenusGestor();
			}else{
		        fcf.showText("Error al canviar l\'ordre",false);	
			}	
			
	 	}
	});

}


